﻿
// ProjectAADlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "ProjectAA.h"
#include "ProjectAADlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

//ImgCap *imgCap;
//ImgFile imgFile; 
//CTS CTSCam;

// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

	// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

	// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CProjectAADlg 对话框



CProjectAADlg::CProjectAADlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_PROJECTAA_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CProjectAADlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CProjectAADlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(ID_32771, &CProjectAADlg::OnOpenLogDlg)
	ON_COMMAND(ID_32773, &CProjectAADlg::OnOpenMotorDlg)
	ON_WM_SIZE()
	ON_COMMAND(ID_32774, &CProjectAADlg::OnOpenTcpipDlg)
	ON_MESSAGE(WM_SEND_SOCKET_SERVER_MSG_TO_MAIN_UI, &CProjectAADlg::OnReceiveServerMsg)
	ON_WM_CTLCOLOR()
	ON_COMMAND(ID_32783, &CProjectAADlg::OnOpenProjectDlg)
	ON_COMMAND(ID_32784, &CProjectAADlg::OnOpenIODlg)
	ON_BN_CLICKED(IDC_BUTTON_HOME, &CProjectAADlg::OnBnClickedButtonHome)
	ON_BN_CLICKED(IDC_CHECK_10AUTO, &CProjectAADlg::OnBnClickedCheck10auto)
	ON_BN_CLICKED(IDC_CHECK_20AUTO, &CProjectAADlg::OnBnClickedCheck20auto)
	ON_COMMAND(ID_32785, &CProjectAADlg::OnOpenControlDlg)
	ON_COMMAND(ID_32786, &CProjectAADlg::OnOpenSettingDlg)
	ON_COMMAND(ID_32787, &CProjectAADlg::OnOpenRS232Dlg)
	ON_BN_CLICKED(IDC_BUTTON1, &CProjectAADlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_CHECK_20AUTO2, &CProjectAADlg::OnBnClickedCheck20auto2)
	ON_BN_CLICKED(IDC_BUTTON2, &CProjectAADlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON_HOME2, &CProjectAADlg::OnBnClickedButtonHome2)
	ON_BN_CLICKED(IDC_BUTTON_HOME3, &CProjectAADlg::OnBnClickedButtonHome3)
	ON_BN_CLICKED(IDC_BUTTON_HOME4, &CProjectAADlg::OnBnClickedButtonHome4)
	ON_BN_CLICKED(IDC_CHECK_20AUTO3, &CProjectAADlg::OnBnClickedCheck20auto3)
END_MESSAGE_MAP()


// CProjectAADlg 消息处理程序

BOOL CProjectAADlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 设置字体参数
	gSystemMgr.m_editFont.CreateFont(40, 0, 4, 0, FW_NORMAL, FALSE, FALSE, 0,
		ANSI_CHARSET,
		OUT_DEFAULT_PRECIS,
		CLIP_DEFAULT_PRECIS,
		DEFAULT_QUALITY,
		DEFAULT_PITCH | FF_SWISS,
		_T("Arial"));

	//轴的比率尺
	//gSystemMgr.m_vCMotor[MOTOR_Z].PositionRatio = 284.44;//3		102400/360

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	gSystemMgr.GeneralSettingIniInit();//读取型号配置

	m_menu.LoadMenuW(IDR_MENU1);
	this->SetMenu(&m_menu);

	//SetUiHandle();

	gSystemMgr.TcpipConfigIniInit();//Tcpip通讯配置
	gSystemMgr.ReadErrorListIni();//错误信息
	gSystemMgr.PosConfigIniInit();//位置配置
	gSystemMgr.RS232ConfigIniInit();
	gSystemMgr.CountConfigIniInit();

	gSystemMgr.m_ptrMainUI = this;

	if (m_cLogDlg == nullptr)
	{
		m_cLogDlg = new CLogDlg;
		m_cLogDlg->Create(IDD_DIALOG_LOG, this);
	}
	m_cLogDlg->ShowWindow(SW_HIDE);

	bool bInit = gSystemMgr.MotorIO_Init();//轴卡初始化
	if (bInit)
	{
		gSystemMgr.StartGetMotorEncoderThread();
		//gSystemMgr.m_cIo.SetAllOutputOff();
		gSystemMgr.m_cIo.StartGetIoInputThread();
		gSystemMgr.m_cIo.SetOutputIo(43, ON);

		gSystemMgr.m_cIo.SetOutputIo(44, ON);
		gSystemMgr.m_cIo.SetOutputIo(46, ON);
		gSystemMgr.m_cIo.SetOutputIo(48, ON);

		//gSystemMgr.m_cIo.SetOutputIo(39, ON);//螺丝枪程序号
		//gSystemMgr.m_cIo.SetOutputIo(40, OFF);
		//gSystemMgr.m_cIo.SetOutputIo(41, OFF);
		//gSystemMgr.m_cIo.SetOutputIo(42, OFF);
	}

	GetClientRect(&m_rcUiRect);

	//Tcpip通信自动建立服务器 或 自动连接
	if (gSystemMgr.m_nModeSelect == 0 && gSystemMgr.m_bAutoFlag) {
		USES_CONVERSION;
		char* c_strIp = T2A(gSystemMgr.m_cstrIPAdd);
		bool bCreateServer = gSystemMgr.m_cSocketServer.OpenSocketServer("Server", c_strIp, gSystemMgr.m_nPort);
		CString cstr;
		cstr.Format(L"Socket Server Build result:%d", bCreateServer);
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_ERROR);
		if (bCreateServer)
		{
			gSystemMgr.StartSocketAcceptClientThread();
		}
	}
	gSystemMgr.m_cClientScan.TcpipMode = 1;

	gSystemMgr.StartTcpipRelinkThread();
	gSystemMgr.StartScanButtonValueThread();
	gSystemMgr.StartOtherThread();
	gSystemMgr.StartAutoThread_10();
	gSystemMgr.StartAutoThread_20();
	gSystemMgr.StartLightControlThread();
	gSystemMgr.StartRS232LinkThread();
	gSystemMgr.StartScrewCheckThread();

	if (gSystemMgr.m_nScrewFlag == 0)
	{
		USES_CONVERSION;
		char* c_strIp = T2A(gSystemMgr.m_cstrIPAdd_screw);
		std::string strIp = c_strIp;
		bool c = gSystemMgr.m_pAtlas.ConnectServer(strIp, gSystemMgr.m_nPort_screw);

		if (!c)
		{
			AfxMessageBox(L"Atlas服务器连接失败，请检查IP地址和端口号是否正确！", MB_ICONERROR);
		}
	}

	CString title = L"";
	GetWindowText(title);
	title.Append(L"-WDR L1 S102030 v1.0.2");
	SetWindowText(title);

	return TRUE;
}

void CProjectAADlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

void CProjectAADlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CRect rect;
		CPaintDC dc(this);
		GetClientRect(rect);
		dc.FillSolidRect(rect, RGB(169, 179, 195));//设置为绿色背景  

		CDialogEx::OnPaint();
	}
}

HCURSOR CProjectAADlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CProjectAADlg::SetUiHandle()
{
	m_menu.EnableMenuItem(ID_32781, MF_DISABLED);
	m_menu.EnableMenuItem(ID_32774, MF_DISABLED);
	m_menu.EnableMenuItem(ID_32773, MF_DISABLED);
	m_menu.EnableMenuItem(ID_32778, MF_DISABLED);
	m_menu.EnableMenuItem(ID_32782, MF_DISABLED);
	m_menu.EnableMenuItem(ID_32776, MF_DISABLED);
	m_menu.EnableMenuItem(ID_32777, MF_DISABLED);
	m_menu.EnableMenuItem(ID_32775, MF_DISABLED);

}


void CProjectAADlg::OnOpenLogDlg()
{
	if (m_cLogDlg != nullptr)
	{
		m_cLogDlg->ShowWindow(SW_SHOW);
	}
}


void CProjectAADlg::OnOpenMotorDlg()
{
	if (m_cMotorDlg != nullptr)
	{
		delete m_cMotorDlg;
		m_cMotorDlg = nullptr;
	}
	if (m_cMotorDlg == nullptr)
	{
		m_cMotorDlg = new CMotorDlg;
		m_cMotorDlg->Create(IDD_DIALOG_MOTOR, this);
	}
	m_cMotorDlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::Repaint(CWnd* pWnd, int last_Width, int now_Width, int last_Height, int now_Height, int cnt)
{
	if (pWnd)
	{
		CRect rect;
		CRect rect2;
		pWnd->GetWindowRect(&rect2);
		rect = OriRect[cnt];
		int n = 62;
		int m = 8;
		//rect2.top = rect2.top - n;
		//rect2.bottom = rect2.bottom - n;
		ScreenToClient(&rect2);

		double proportion_x = now_Width / (double)last_Width;
		double proportion_y = now_Height / (double)last_Height;

		rect.left = (long)(rect.left * proportion_x + 0.5 - m * proportion_x);
		rect.right = (long)(rect.left + RectW[cnt] * proportion_x + 0.5);
		rect.top = (long)(rect.top * proportion_y + 0.5 - n * proportion_y);
		rect.bottom = (long)(rect.top + RectH[cnt] * proportion_y + 0.5);
		pWnd->MoveWindow(&rect);
	}

}

void CProjectAADlg::OnSize(UINT nType, int cx, int cy)
{
	CDialogEx::OnSize(nType, cx, cy);

	if (0 == m_rcUiRect.left && 0 == m_rcUiRect.right && 0 == m_rcUiRect.top && 0 == m_rcUiRect.bottom)//第一次启动对话框时的大小变化不做处理
	{
	}
	else
	{
		if (0 == cx && 0 == cy)//如果是按下了最小化，则触发条件，这时不保存对话框数据
		{
			return;
		}
		CRect rectDlgChangeSize;
		GetClientRect(&rectDlgChangeSize);//存储对话框大小改变后对话框大小数据
	}
	//GetClientRect(&m_rcUiRect); //save size of dialog
	Invalidate();//更新窗口
}


void CProjectAADlg::OnOpenTcpipDlg()
{
	if (m_cTcpipDlg != nullptr)
	{
		delete m_cTcpipDlg;
		m_cTcpipDlg = nullptr;
	}
	if (m_cTcpipDlg == nullptr)
	{
		m_cTcpipDlg = new CTcpipDlg;
		m_cTcpipDlg->Create(IDD_DIALOG_TCPIP, this);
	}
	m_cTcpipDlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::OnCancel()
{
	gSystemMgr.m_cSocketServer.CloseSocketServer();
	//KillTimer(TIMER_ID_IO_CONTROL_STATUS);

	delete m_cMotorDlg;
	delete m_cLogDlg;
	delete m_cTcpipDlg;
	delete m_cProjectDlg;

	CString tcpmsg;
	tcpmsg.Format(L"STOP\r\n");
	gSystemMgr.m_cClientYamaha.m_sendBuffer.push_front(tcpmsg.GetBuffer());
	Sleep(200);
	gSystemMgr.m_cClientYamaha.CloseClient();

	dmc_emg_stop(gSystemMgr.m_nCard);
	dmc_board_close();//释放资源
	CDialogEx::OnCancel();
}

LRESULT CProjectAADlg::OnReceiveServerMsg(WPARAM wParam, LPARAM lParam)
{
	CString cstrp = (BSTR)wParam;
	SysFreeString((BSTR)wParam);

	gSystemLog.DisplayLogAndSave(cstrp.GetBuffer(), LV_EVENT);

	std::vector<wstring> vResultItem = _utils::SplitString(cstrp.GetBuffer(), L",");

	if (vResultItem.size() < 2)
	{
		gSystemLog.DisplayLogAndSave(L"Client 返回的数据有误", LV_ERROR);
		return LRESULT();
	}

	return LRESULT();
}
HBRUSH CProjectAADlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr1 = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);

	DeleteObject(hbr);

	UINT id = pWnd->GetDlgCtrlID();
	CString Cstr;
	if (id == IDC_STATIC_2)
	{
		hbr = CreateSolidBrush(RGB(169, 179, 195));
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_CHECK_10AUTO)
	{
		if (gSystemMgr.m_bAutoFlag_10) {
			hbr = CreateSolidBrush(RGB(0, 255, 0));
			pDC->SetTextColor(RGB(0, 255, 0));
		}
		else {
			hbr = CreateSolidBrush(RGB(255, 0, 0));
			pDC->SetTextColor(RGB(255, 0, 0));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_CHECK_20AUTO)
	{
		if (gSystemMgr.m_bAutoFlag_20) {
			hbr = CreateSolidBrush(RGB(0, 255, 0));
			pDC->SetTextColor(RGB(0, 255, 0));
		}
		else {
			hbr = CreateSolidBrush(RGB(255, 0, 0));
			pDC->SetTextColor(RGB(255, 0, 0));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_CHECK_20AUTO2)
	{
		if (gSystemMgr.m_bAutoFlag_20_2) {
			hbr = CreateSolidBrush(RGB(0, 255, 0));
			pDC->SetTextColor(RGB(0, 255, 0));
		}
		else {
			hbr = CreateSolidBrush(RGB(255, 0, 0));
			pDC->SetTextColor(RGB(255, 0, 0));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_CHECK_20AUTO3)
	{
		if (gSystemMgr.m_bAutoFlag_30) {
			hbr = CreateSolidBrush(RGB(0, 255, 0));
			pDC->SetTextColor(RGB(0, 255, 0));
		}
		else {
			hbr = CreateSolidBrush(RGB(255, 0, 0));
			pDC->SetTextColor(RGB(255, 0, 0));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_60)
	{
		if (gSystemMgr.m_bHomeFlag) {
			hbr = CreateSolidBrush(RGB(0, 255, 0));
			pDC->SetBkMode(TRANSPARENT);
		}
		else {
			hbr = CreateSolidBrush(RGB(255, 0, 0));
			pDC->SetBkMode(TRANSPARENT);
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_66)
	{
		if (!gSystemMgr.m_bMotorErrorFlag) {
			hbr = CreateSolidBrush(RGB(0, 255, 0));
			pDC->SetBkMode(TRANSPARENT);
		}
		else {
			hbr = CreateSolidBrush(RGB(255, 0, 0));
			pDC->SetBkMode(TRANSPARENT);
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_109)
	{
		if (gSystemMgr.m_nPassRfidFlag) {
			hbr = CreateSolidBrush(RGB(255, 0, 0));
			pDC->SetBkMode(TRANSPARENT);
		}
		else {
			hbr = CreateSolidBrush(RGB(0, 255, 0));
			pDC->SetBkMode(TRANSPARENT);
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_115)//位移传感器
	{
		if (gSystemMgr.ComWeiyiConnectFlag) {
			hbr = CreateSolidBrush(RGB(0, 255, 0));
			pDC->SetBkMode(TRANSPARENT);
		}
		else {
			hbr = CreateSolidBrush(RGB(255, 0, 0));
			pDC->SetBkMode(TRANSPARENT);
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_116)//螺丝枪
	{
		if (gSystemMgr.ComScrewConnectFlag) {
			hbr = CreateSolidBrush(RGB(0, 255, 0));
			pDC->SetBkMode(TRANSPARENT);
		}
		else {
			hbr = CreateSolidBrush(RGB(255, 0, 0));
			pDC->SetBkMode(TRANSPARENT);
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	//rfid
	else if (id == IDC_STATIC_68)
	{
		if (gSystemMgr.Com1ConnectFlag) {
			hbr = CreateSolidBrush(RGB(0, 255, 0));
			pDC->SetBkMode(TRANSPARENT);
		}
		else {
			hbr = CreateSolidBrush(RGB(255, 0, 0));
			pDC->SetBkMode(TRANSPARENT);
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_69)
	{
		if (gSystemMgr.Com2ConnectFlag) {
			hbr = CreateSolidBrush(RGB(0, 255, 0));
			pDC->SetBkMode(TRANSPARENT);
		}
		else {
			hbr = CreateSolidBrush(RGB(255, 0, 0));
			pDC->SetBkMode(TRANSPARENT);
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_97)
	{
		if (gSystemMgr.Com3ConnectFlag) {
			hbr = CreateSolidBrush(RGB(0, 255, 0));
			pDC->SetBkMode(TRANSPARENT);
		}
		else {
			hbr = CreateSolidBrush(RGB(255, 0, 0));
			pDC->SetBkMode(TRANSPARENT);
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_120)
	{
		if (gSystemMgr.Com4ConnectFlag) {
			hbr = CreateSolidBrush(RGB(0, 255, 0));
			pDC->SetBkMode(TRANSPARENT);
		}
		else {
			hbr = CreateSolidBrush(RGB(255, 0, 0));
			pDC->SetBkMode(TRANSPARENT);
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_98)//tcpip
	{
		if (gSystemMgr.m_cClientYamaha.m_bConnect)
		{
			hbr = CreateSolidBrush(RGB(0, 255, 0));
		}
		else
		{
			hbr = CreateSolidBrush(RGB(255, 0, 0));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_108)//tcpip
	{
		if (gSystemMgr.m_cClientCam.m_bConnect)
		{
			hbr = CreateSolidBrush(RGB(0, 255, 0));
		}
		else
		{
			hbr = CreateSolidBrush(RGB(255, 0, 0));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_102)//tcpip
	{
		if (gSystemMgr.m_cClientScan.m_bConnect)
		{
			hbr = CreateSolidBrush(RGB(0, 255, 0));
		}
		else
		{
			hbr = CreateSolidBrush(RGB(255, 0, 0));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_121)//tcpip
	{
		//if (gSystemMgr.m_cClientScrew.m_bConnect)
		if (gSystemMgr.m_pAtlas.IsConnected())
		{
			hbr = CreateSolidBrush(RGB(0, 255, 0));
		}
		else
		{
			hbr = CreateSolidBrush(RGB(255, 0, 0));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_101)//上层
	{
		if (gSystemMgr.TopPallectInFlag)
		{
			hbr = CreateSolidBrush(RGB(0, 255, 0));
		}
		else
		{
			hbr = CreateSolidBrush(RGB(255, 0, 0));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else
	{
		hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

	}
	hbr1 = hbr;
	return hbr1;
}


void CProjectAADlg::OnOpenProjectDlg()
{
	if (m_cProjectDlg != nullptr)
	{
		delete m_cProjectDlg;
		m_cProjectDlg = nullptr;
	}
	if (m_cProjectDlg == nullptr)
	{
		m_cProjectDlg = new CChangeProjectDlg;
		m_cProjectDlg->Create(IDD_DIALOG_PROJECT, this);
	}
	m_cProjectDlg->ShowWindow(SW_SHOW);
}



void CProjectAADlg::OnOpenIODlg()
{
	if (m_cIODlg != nullptr)
	{
		delete m_cIODlg;
		m_cIODlg = nullptr;
	}
	if (m_cIODlg == nullptr)
	{
		m_cIODlg = new CIODlg;
		m_cIODlg->Create(IDD_DIALOG_IO, this);
	}
	m_cIODlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::OnBnClickedButtonHome()
{
	gSystemLog.DisplayLogAndSave(L"按下回原点按钮", LV_EVENT);
	if (gSystemMgr.m_bHomeFlag == 0)
		gSystemMgr.m_nButtonValue = 10;
}


void CProjectAADlg::OnBnClickedCheck10auto()
{
	CString cstr;
	CButton* pCheckBox = (CButton*)GetDlgItem(IDC_CHECK_10AUTO);
	int nFlag = pCheckBox->GetCheck();
	if (nFlag == BST_CHECKED)
	{
		gSystemMgr.m_bAutoFlag_10 = 1;
		gSystemMgr.m_cIo.SetOutputIo(58, TRUE);
		gSystemMgr.m_cIo.SetOutputIo(59, TRUE);
		cstr.Append(L"开启上料自动");
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
		GetDlgItem(IDC_CHECK_10AUTO)->SetWindowTextW(L"10自动开启");
	}
	if (nFlag == BST_UNCHECKED)
	{
		gSystemMgr.m_bAutoFlag_10 = 0;
		cstr.Append(L"关闭上料自动");
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
		GetDlgItem(IDC_CHECK_10AUTO)->SetWindowTextW(L"10自动关闭");
	}
}


void CProjectAADlg::OnBnClickedCheck20auto()
{
	CString cstr;
	CButton* pCheckBox = (CButton*)GetDlgItem(IDC_CHECK_20AUTO);
	int nFlag = pCheckBox->GetCheck();
	if (nFlag == BST_CHECKED)
	{
		gSystemMgr.m_bAutoFlag_20 = 1;
		cstr.Append(L"开启20打螺丝自动");
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
		GetDlgItem(IDC_CHECK_20AUTO)->SetWindowTextW(L"20打螺丝自动开启");
	}
	if (nFlag == BST_UNCHECKED)
	{
		gSystemMgr.m_bAutoFlag_20 = 0;
		cstr.Append(L"关闭20打螺丝自动");
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
		GetDlgItem(IDC_CHECK_20AUTO)->SetWindowTextW(L"20打螺丝自动关闭");
	}
}


void CProjectAADlg::OnOpenControlDlg()
{
	if (m_cControlDlg != nullptr)
	{
		delete m_cControlDlg;
		m_cControlDlg = nullptr;
	}
	if (m_cControlDlg == nullptr)
	{
		m_cControlDlg = new CControlDlg;
		m_cControlDlg->Create(IDD_DIALOG_CONTROL, this);
	}
	m_cControlDlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::OnOpenSettingDlg()
{
	if (m_cSettingDlg != nullptr)
	{
		delete m_cSettingDlg;
		m_cSettingDlg = nullptr;
	}
	if (m_cSettingDlg == nullptr)
	{
		m_cSettingDlg = new CSettingDlg;
		m_cSettingDlg->Create(IDD_DIALOG_SETTING, this);
	}
	m_cSettingDlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::OnOpenRS232Dlg()
{
	if (m_cRS232Dlg != nullptr)
	{
		delete m_cRS232Dlg;
		m_cRS232Dlg = nullptr;
	}
	if (m_cRS232Dlg == nullptr)
	{
		m_cRS232Dlg = new CRS232Dlg;
		m_cRS232Dlg->Create(IDD_DIALOG_RS232, this);
	}
	m_cRS232Dlg->ShowWindow(SW_SHOW);
}


BOOL CProjectAADlg::PreTranslateMessage(MSG* pMsg)
{
	if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_SPACE) return TRUE; //如果是空格消息，返回；
	if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE) return TRUE; //如果是Esc消息，返回；

	//if (gSystemMgr.m_nGetCodeFlag != 1 && pMsg->message == WM_KEYDOWN) {
	//	USES_CONVERSION;
	//	if (pMsg->wParam == VK_RETURN && gSystemMgr.m_nBarcodeLength > 0)
	//	{
	//		if (gSystemMgr.CheckFlag1) {
	//			char ch[20] = { 0 };
	//			sprintf(ch, "%s", T2A(gSystemMgr.CheckSN1));
	//			int length = strlen(ch);
	//			int flag = 0;
	//			for (int i = 0; i < length; i++) {
	//				if (ch[i] != gSystemMgr.Barcode[i]) {
	//					flag = 1;
	//				}
	//			}
	//			if (flag == 1) {
	//				gSystemMgr.m_nBarcodeLength = 0;
	//				memset(gSystemMgr.Barcode, 0, sizeof(gSystemMgr.Barcode));
	//				gSystemLog.DisplayLogAndSave(L"条码1异常", LV_EVENT);
	//				gSystemMgr.m_nGetCodeFlag = -1;
	//				return true;
	//			}
	//		}
	//		gSystemLog.DisplayLogAndSave(A2T(gSystemMgr.Barcode), LV_EVENT);
	//		SetDlgItemTextW(IDC_STATIC_10SN, A2T(gSystemMgr.Barcode));
	//		gSystemMgr.m_nGetCodeFlag = 1;
	//		return true;
	//	}

	//	if (gSystemMgr.m_nBarcodeLength >= 50) {
	//		gSystemMgr.m_nBarcodeLength = 0;
	//		memset(gSystemMgr.Barcode, 0, sizeof(gSystemMgr.Barcode));
	//		gSystemMgr.m_nGetCodeFlag = -1;
	//		return true;
	//	}
	//	switch (pMsg->wParam)
	//	{
	//	case VK_A:
	//	case VK_B:
	//	case VK_C:
	//	case VK_D:
	//	case VK_E:
	//	case VK_F:
	//	case VK_G:
	//	case VK_H:
	//	case VK_I:
	//	case VK_J:
	//	case VK_K:
	//	case VK_L:
	//	case VK_M:
	//	case VK_N:
	//	case VK_O:
	//	case VK_P:
	//	case VK_Q:
	//	case VK_R:
	//	case VK_S:
	//	case VK_T:
	//	case VK_U:
	//	case VK_V:
	//	case VK_W:
	//	case VK_X:
	//	case VK_Y:
	//	case VK_Z:
	//	case VK_0:
	//	case VK_1:
	//	case VK_2:
	//	case VK_3:
	//	case VK_4:
	//	case VK_5:
	//	case VK_6:
	//	case VK_7:
	//	case VK_8:
	//	case VK_9:
	//		gSystemMgr.Barcode[gSystemMgr.m_nBarcodeLength] = pMsg->wParam;
	//		gSystemMgr.m_nBarcodeLength++;
	//		break;
	//	}
	//	//gSystemMgr.Barcode[gSystemMgr.m_nBarcodeLength] = pMsg->wParam;
	//	//gSystemMgr.m_nBarcodeLength++;
	//	if (gSystemMgr.m_nBarcodeLength > 0)
	//	{
	//		SetDlgItemTextW(IDC_STATIC_10SN, A2T(gSystemMgr.Barcode));
	//	}
	//}

	//if (gSystemMgr.m_nGetCodeFlag == 1 && gSystemMgr.m_nGetCodeFlag_2 != 1 && pMsg->message == WM_KEYDOWN && gSystemMgr.m_bSecondCode == 1) {
	//	USES_CONVERSION;
	//	if (pMsg->wParam == VK_RETURN && gSystemMgr.m_nBarcodeLength_2 > 0)
	//	{
	//		if (gSystemMgr.CheckFlag2) {
	//			char ch[20] = { 0 };
	//			sprintf(ch, "%s", T2A(gSystemMgr.CheckSN2));
	//			int length = strlen(ch);
	//			int flag = 0;
	//			for (int i = 0; i < length; i++) {
	//				if (ch[i] != gSystemMgr.Barcode_2[i]) {
	//					flag = 1;
	//				}
	//			}
	//			if (flag == 1) {
	//				gSystemMgr.m_nBarcodeLength_2 = 0;
	//				memset(gSystemMgr.Barcode_2, 0, sizeof(gSystemMgr.Barcode_2));
	//				gSystemLog.DisplayLogAndSave(L"条码2异常", LV_EVENT);
	//				gSystemMgr.m_nGetCodeFlag_2 = -1;
	//				return true;
	//			}
	//		}
	//		gSystemLog.DisplayLogAndSave(A2T(gSystemMgr.Barcode_2), LV_EVENT);
	//		SetDlgItemTextW(IDC_STATIC_10SN2, A2T(gSystemMgr.Barcode_2));
	//		gSystemMgr.m_nGetCodeFlag_2 = 1;
	//		return true;
	//	}

	//	if (gSystemMgr.m_nBarcodeLength_2 >= 50) {
	//		gSystemMgr.m_nBarcodeLength_2 = 0;
	//		memset(gSystemMgr.Barcode_2, 0, sizeof(gSystemMgr.Barcode_2));
	//		gSystemMgr.m_nGetCodeFlag_2 = -1;
	//		return true;
	//	}
	//	switch (pMsg->wParam)
	//	{
	//	case VK_A:
	//	case VK_B:
	//	case VK_C:
	//	case VK_D:
	//	case VK_E:
	//	case VK_F:
	//	case VK_G:
	//	case VK_H:
	//	case VK_I:
	//	case VK_J:
	//	case VK_K:
	//	case VK_L:
	//	case VK_M:
	//	case VK_N:
	//	case VK_O:
	//	case VK_P:
	//	case VK_Q:
	//	case VK_R:
	//	case VK_S:
	//	case VK_T:
	//	case VK_U:
	//	case VK_V:
	//	case VK_W:
	//	case VK_X:
	//	case VK_Y:
	//	case VK_Z:
	//	case VK_0:
	//	case VK_1:
	//	case VK_2:
	//	case VK_3:
	//	case VK_4:
	//	case VK_5:
	//	case VK_6:
	//	case VK_7:
	//	case VK_8:
	//	case VK_9:
	//		gSystemMgr.Barcode_2[gSystemMgr.m_nBarcodeLength_2] = pMsg->wParam;
	//		gSystemMgr.m_nBarcodeLength_2++;
	//		break;
	//	}
	//	if (gSystemMgr.m_nBarcodeLength_2 > 0)
	//	{
	//		SetDlgItemTextW(IDC_STATIC_10SN2, A2T(gSystemMgr.Barcode_2));
	//	}
	//}

	//if (pMsg->message == WM_KEYDOWN && IsDialogMessage(HWND_MAIN, &msg) && maindlg.count_bcd < 19)//判断是否有按键按下
	//{
	//	changeTochar(msg.wParam);
	//}
	if (pMsg->message == WM_KEYDOWN/*pMsg->wParam == VK_RETURN*/) {
		return true;
	}


	return CDialogEx::PreTranslateMessage(pMsg);
}


void CProjectAADlg::OnBnClickedButton1()
{
	SetDlgItemTextW(IDC_STATIC_10SN, L"...");
	gSystemMgr.m_nGetCodeFlag = 0;
	memset(gSystemMgr.Barcode, 0, sizeof(gSystemMgr.Barcode));
	gSystemMgr.m_nBarcodeLength = 0;
}


void CProjectAADlg::OnBnClickedCheck20auto2()
{
	CString cstr;
	CButton* pCheckBox = (CButton*)GetDlgItem(IDC_CHECK_20AUTO2);
	int nFlag = pCheckBox->GetCheck();
	if (nFlag == BST_CHECKED)
	{
		gSystemMgr.m_bAutoFlag_20_2 = 1;
		cstr.Append(L"开启20上料自动");
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
		GetDlgItem(IDC_CHECK_20AUTO2)->SetWindowTextW(L"20上料自动开启");
	}
	if (nFlag == BST_UNCHECKED)
	{
		gSystemMgr.m_bAutoFlag_20_2 = 0;
		cstr.Append(L"关闭20上料自动");
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
		GetDlgItem(IDC_CHECK_20AUTO2)->SetWindowTextW(L"20上料自动关闭");
	}
}


void CProjectAADlg::OnBnClickedButton2()
{
	SetDlgItemTextW(IDC_STATIC_10SN2, L"...");
	gSystemMgr.m_nGetCodeFlag_2 = 0;
	memset(gSystemMgr.Barcode_2, 0, sizeof(gSystemMgr.Barcode_2));
	gSystemMgr.m_nBarcodeLength_2 = 0;
}


void CProjectAADlg::OnBnClickedButtonHome2()
{
	if (IDYES == AfxMessageBox(L"确定 清空当前统计信息 吗？", MB_YESNO))
	{
		CString cstr;
		cstr.Format(L"统计信息: 总数:%d  OK数:%d", gSystemMgr.TotalCount, gSystemMgr.OKCount);
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_ERROR);
		gSystemMgr.TotalCount = 0;
		gSystemMgr.OKCount = 0;
	}
}


void CProjectAADlg::OnBnClickedButtonHome3()
{
	if (IDYES == AfxMessageBox(L"确定 清空批头使用次数 吗？", MB_YESNO))
	{
		gSystemMgr.m_nPitouCount = 0;
	}
}


void CProjectAADlg::OnBnClickedButtonHome4()
{
	if (gSystemMgr.m_nPassRfidFlag) {
		if (IDYES == AfxMessageBox(L"确定 开启RFID 吗？", MB_YESNO | MB_ICONWARNING))
		{
			gSystemMgr.m_nPassRfidFlag = 0;
		}
	}
	else {
		if (IDYES == AfxMessageBox(L"确定 屏蔽RFID 吗？", MB_YESNO | MB_ICONWARNING))
		{
			gSystemMgr.m_nPassRfidFlag = 1;
		}
	}
}


void CProjectAADlg::OnBnClickedCheck20auto3()
{
	CString cstr;
	CButton* pCheckBox = (CButton*)GetDlgItem(IDC_CHECK_20AUTO3);
	int nFlag = pCheckBox->GetCheck();
	if (nFlag == BST_CHECKED)
	{
		gSystemMgr.m_bAutoFlag_30 = 1;
		cstr.Append(L"开启30站自动");
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
		GetDlgItem(IDC_CHECK_20AUTO3)->SetWindowTextW(L"30站自动开启");
	}
	if (nFlag == BST_UNCHECKED)
	{
		gSystemMgr.m_bAutoFlag_30 = 0;
		cstr.Append(L"关闭30站自动");
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
		GetDlgItem(IDC_CHECK_20AUTO3)->SetWindowTextW(L"30站自动关闭");
	}
}
